//package com.touchfired.common.event.handler.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.touchfired.common.constants.ClientInfo;
//import com.touchfired.common.constants.EventCode;
//import com.touchfired.common.constants.GlobalConstants;
//import com.touchfired.common.exception.ErrorCode;
//import com.touchfired.common.exception.ServiceException;
//import com.touchfired.common.message.BaseMessage;
//import com.touchfired.common.message.transfer.MessageSender;
//import io.netty.channel.ChannelHandlerContext;
//import org.apache.commons.cli.*;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Component;
//
//import java.util.Collection;
//
///**
// * @Description 连接成功事件码处理器
// * @Author 幸佳俊
// */
//@Component
//public class EventCodeRoomSettingsHandler extends BaseEventCodeHandler {
//
//    public final static String P = "p";
//    public final static String PLAY = "play";
//    public final static String S = "s";
//    public final static String SPY = "spy";
//    public final static String N = "n";
//    public final static String NAME = "name";
//    final static Integer MIN_PLAYER = 3;
//
//    public EventCodeRoomSettingsHandler() {
//        super(EventCode.ROOM_SETTINGS);
//    }
//
//    @Override
//    public EventCodeHandler doHandle(ChannelHandlerContext ctx, ClientInfo clientInfo, BaseMessage message) throws Exception {
//        Options options = new Options();
//        options.addRequiredOption(P, PLAY, true,"玩家人数");
//        options.addRequiredOption(S, SPY, true,"卧底人数");
//        options.addOption(N, NAME, true,"房间名称");
//
//        try {
//            String content = message.getContent();
//            if (StringUtils.isNotBlank(content)) {
//                if (GlobalConstants.BACK.equalsIgnoreCase(content)) {
//                    return eventHandle(ctx, clientInfo, message, EventCode.PADDING_JOINING);
//                }
//
//                String roomName = null;
//                CommandLineParser parser = new DefaultParser();
//                CommandLine cmd = parser.parse(options, content.split(" "));
//
//                // 参数校验
//                if (!cmd.hasOption(P) && cmd.getOptionValue(P) == null) {
//                    throw new ServiceException(ErrorCode.ROOM_SETTINGS_PLAYER_NULL);
//                }
//                if (cmd.hasOption(S) && cmd.getOptionValue(S) == null) {
//                    throw new ServiceException(ErrorCode.ROOM_SETTINGS_SPYER_NULL);
//                }
//                if (cmd.hasOption(N) && cmd.getOptionValue(N) != null) {
//                    roomName = cmd.getOptionValue(N);
//                }
//
//                // 人数校验
//                int optionPlayerVal = Integer.parseInt(cmd.getOptionValue(P));
//                int optionSpyerVal = Integer.parseInt(cmd.getOptionValue(S));
//                if (optionPlayerVal < MIN_PLAYER) {
//                    throw new ServiceException(ErrorCode.ROOM_SETTINGS_PLAYER_NULL.getCode(),
//                            ErrorCode.ROOM_SETTINGS_PLAYER_NULL.getDesc() + "，必须 ≥ " + MIN_PLAYER + "人。");
//                }
//                if (optionPlayerVal <= optionSpyerVal) {
//                    throw new ServiceException(ErrorCode.ROOM_SETTINGS_SPYER_NULL.getCode(),
//                            ErrorCode.ROOM_SETTINGS_SPYER_NULL.getDesc() + "，必须 ≤ 总玩家人数。");
//                }
//
//                BaseMessage newMessage = (BaseMessage) message.clone();
//                JSONObject otherMap = new JSONObject();
//                otherMap.put(PLAY, optionPlayerVal);
//                otherMap.put(SPY, optionSpyerVal);
//                otherMap.put(NAME, roomName);
//                newMessage.setOtherMap(otherMap);
//                return eventHandle(ctx, clientInfo, newMessage, EventCode.ROOM_CREATED);
//            }
//        } catch (ServiceException se) {
//            MessageSender.error(ctx.channel(), message, se.getDesc());
//        } catch (NumberFormatException ne) {
//            MessageSender.error(ctx.channel(), message, ErrorCode.ERROR_OPTIONS_INPUT_VALUE.getDesc());
//        } catch (ParseException | CloneNotSupportedException ignored) {
//        }
//
//        String createTips = "\n" +
//                getOptionsStr(options) + "\n" +
//                "[注]输入‘" + GlobalConstants.BACK + "’回退上一级。";
//        MessageSender.info(ctx.channel(), message, createTips);
//        return this;
//    }
//
//    private String getOptionsStr(Options options) {
//        StringBuilder result = new StringBuilder();
//        if (options != null) {
//            Collection<Option> optList = options.getOptions();
//            optList.forEach(opt -> {
//                result.append("-")
//                        .append(opt.getOpt())
//                        .append(opt.getDescription())
//                        .append(opt.isRequired() ? "[必须]" : "")
//                        .append("  ");
//            });
//        }
//        return result.toString();
//    }
//}
